<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreService" %>
<%@ page import="com.google.appengine.api.datastore.Query" %>
<%@ page import="com.google.appengine.api.datastore.Entity" %>
<%@ page import="com.google.appengine.api.datastore.FetchOptions" %>
<%@ page import="com.google.appengine.api.datastore.Key" %>
<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>

<html>
  <head>
    <link type="text/css" rel="stylesheet" href="/static/main.css" />
  </head>

  <body>

<%
    String nombreCuenta = request.getParameter("nombreCuenta");
    if (nombreCuenta == null) {
    	nombreCuenta = "default";
    }
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    if (user != null) {
%>
<p>Hello, <%= user.getNickname() %>! (You can
<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">sign out</a>.)</p>
<%
    } else {
%>
<p>Hello!
<a href="<%= userService.createLoginURL(request.getRequestURI()) %>">Sign in</a>
to include your name with greetings you post.</p>
<%
    }
%>

<%
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Key guestbookKey = KeyFactory.createKey("Cuenta", nombreCuenta);
    // Run an ancestor query to ensure we see the most up-to-date
    // view of the Greetings belonging to the selected Guestbook.
    Query query = new Query("Cuenta", guestbookKey).addSort("fechaCreacion", Query.SortDirection.DESCENDING);
    List<Entity> cuentas = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(5));
    if (cuentas.isEmpty()) {
        %>
        <p>Nombre de Cuenta '<%= nombreCuenta %>' has no messages.</p>
        <%
    } else {
        %>
        <p>Messages in nombreCuenta '<%= nombreCuenta %>'.</p>
        <%
        for (Entity cuenta : cuentas) {
            if (cuenta.getProperty("user") == null) {
                %>
                <p>An anonymous person wrote:</p>
                <%
            } else {
                %>
                <p><b><%= ((User) cuenta.getProperty("user")).getNickname() %></b> wrote:</p>
                <%
            }
            %>
            <blockquote><%= cuenta.getProperty("concepto") %></blockquote>
            <%
        }
    }
%>

    <form action="/sign" method="post">
		<div>Capital: <input type="text" name="capital" /></div>
		<div>Interes Porcentaje: <input type="text" name="interesPorcentaje" /></div>
		<div>Interes: <input type="text" name="interes" /></div>
		<div>Interes Adeudado: <input type="text" name="interesAdeudado" /></div>
		<div>Ganancia: <input type="text" name="ganancia" /></div>
		<div>Nombre Cuenta: <input type="text" name="nombreCuenta" /></div>
		<div>Concepto: <input type="text" name="concepto" /></div>
		<div>Fecha de Prestamo: <input type="text" name="fechaPrestamo" /></div>
		<div><input type="submit" value="Crear Cuenta" /></div>
		<input type="hidden" name="nombreCuenta" value="<%= nombreCuenta %>"/>
    </form>

  </body>
</html>